﻿//BOM Tool 主界面
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BOM.dao;
using BOM.entity;
using System.Collections;


namespace BOM
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
            initLeftTree();
        }
        //初始化左侧树
        public void initLeftTree()
        {

            Bom rootBom = BomDaoImp.getRootNodeBom();
            if (rootBom == null)
                return;
            TreeNode rootNode = new TreeNode(rootBom.getPartName());
            rootNode.Tag = rootBom;
            //加载根节点其下2层级的节点，使第二层包含子节点的节点显示带+号
            add2LevelChildNodes(rootNode);
            leftTree.Nodes.Clear();
            leftTree.Nodes.Add(rootNode);
            leftTree.SelectedNode = rootNode;
            leftTree.ExpandAll();
        }
        //加载当前节点下两级节点元素
        public void add2LevelChildNodes(TreeNode curNode)
        {
            Bom curBom = (Bom)curNode.Tag;
            List<Bom> childBoms = BomDaoImp.getChildBomsByParentId(curBom.getId());
            if (childBoms == null || childBoms.Count == 0)
                return;
            for (int i = 0; i < childBoms.Count; i++)
            {
                TreeNode tempNode = new TreeNode(childBoms[i].getPartName());
                tempNode.Tag = childBoms[i];
                curNode.Nodes.Add(tempNode);
                addChildNodes(tempNode);
            }
        }
        //加载当前节点子节点元素
        public void addChildNodes(TreeNode curNode)
        {
            Bom curBom = (Bom)curNode.Tag;
            List<Bom> childBoms = BomDaoImp.getChildBomsByParentId(curBom.getId());
            if (childBoms == null || childBoms.Count == 0)
                return;
            for (int i = 0; i < childBoms.Count; i++)
            {
                TreeNode tempNode = new TreeNode(childBoms[i].getPartName());
                tempNode.Tag = childBoms[i];
                curNode.Nodes.Add(tempNode);
            }
        }
        private void toolStripButton2_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Bom bom = BomDaoImp.getBomById(1);
            Console.WriteLine(bom.getPartName());
        }

        //树节点选择事件响应
        public void leftTree_afterSelectNode(object sender, TreeViewEventArgs e)
        {
            TreeNode selectNode = e.Node;
            refreshDataTable(selectNode);
        }
        //根据选中树节点刷新右侧数据列表数据
        public void refreshDataTable(TreeNode selectNode)
        {
            int id = ((Bom)selectNode.Tag).getId();
            
            // fillDataToTable(selectNode,dataView);
            List<Bom> list = BomDaoImp.getChildBomsAndSelfByParentId(id);
            fillDataListToTable(list,dataView);
        }
        //填充数据到表格
        public void fillDataListToTable(List<Bom> list, DataGridView table)
        {
            if (list == null || list.Count == 0)
            {
                table.Rows.Clear();
                return;
            }
            else
            {
                table.Rows.Clear();
                for (int i = 0; i < list.Count; i++)
                {
                    fillDataToTable(list[i], dataView);
                }
                if (table.RowCount > 0)
                {
                    table.Rows[0].DefaultCellStyle.BackColor = Color.LightGray;
                }
            }
        }
        private void fillDataToTable(Bom bom,DataGridView table)
        {
            //Bom bom = (Bom)node.Tag;
            int rowIndex = table.Rows.Add();
            table.Rows[rowIndex].Tag = bom;
            table.Rows[rowIndex].Cells["seq"].Value = rowIndex+1;
            table.Rows[rowIndex].Cells["id"].Value = bom.getId();
            table.Rows[rowIndex].Cells["partNumber"].Value = bom.getPartNumber();
            table.Rows[rowIndex].Cells["partName"].Value = bom.getPartName();
            table.Rows[rowIndex].Cells["darawingNo"].Value = bom.getDarawingNo();
            table.Rows[rowIndex].Cells["naterialCost"].Value = bom.getNaterialCost();
            table.Rows[rowIndex].Cells["labourCost"].Value = bom.getLabourCost();
            table.Rows[rowIndex].Cells["totalCost"].Value = bom.getTotalCost();
            table.Rows[rowIndex].Cells["qty"].Value = bom.getQty();
            table.Rows[rowIndex].Cells["SumCost"].Value = bom.getSumCost();
            Image img ;
            if (1 == bom.getType())
                img = BOM.Properties.Resources.component;
            else if (2 == bom.getType())
                img = BOM.Properties.Resources.part;
            else if (3 == bom.getType())
                img = BOM.Properties.Resources.doc;
            else
                img = null;
            table.Rows[rowIndex].Cells["icon"].Value = img;
        }


        //树节点展开事件，加载当前节点下2层级子节点
        private void leftTree_afterExpand(object sender, TreeViewEventArgs e)
        {
            TreeNode selectNode = e.Node;
            selectNode.Nodes.Clear();
            add2LevelChildNodes(selectNode);
        }
        //刷新当前节点
        public void refreshCurSelectedNode(TreeNode curNode)
        {
            curNode.Nodes.Clear();
            add2LevelChildNodes(curNode);
        }
        //新增按钮事件
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            Hashtable ctx = new Hashtable();
            //新增状态
            ctx.Add("oprtstate",1);
            if (leftTree.SelectedNode == null)
                leftTree.SelectedNode =leftTree.Nodes[0];
            //当前节点参数 
            ctx.Add("curnode",leftTree.SelectedNode);
            openEditUI(ctx);
        }
        //修改按钮
        private void btnEdit_Click(object sender, EventArgs e)
        {
           int id = (int)dataView.SelectedRows[0].Cells["id"].Value;
           Hashtable ctx = new Hashtable();
           ctx.Add("oprtstate",2); //修改状态
           if (leftTree.SelectedNode == null)
               leftTree.SelectedNode = leftTree.Nodes[0];
           //当前节点参数 
           ctx.Add("curnode", leftTree.SelectedNode);
           ctx.Add("id", id);
           openEditUI(ctx);

        }
        //弹出详细信息界面
        public void openEditUI(Hashtable ctx)
        {
            BomForm bomForm = new BomForm(ctx);
            bomForm.ShowDialog(this);
            refreshCurSelectedNode(leftTree.SelectedNode);
           // this.refreshCurSelectedNode(leftTree.SelectedNode);
          refreshDataTable(leftTree.SelectedNode);
        }
        //删除按钮事件
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataView.SelectedRows == null)
            {
                MessageBox.Show("没有选中记录");
                return;
            }
            if(DialogResult.Yes == MessageBox.Show(this,"是否确认要该记录信息？","提示",MessageBoxButtons.YesNo, MessageBoxIcon.Question,MessageBoxDefaultButton.Button1))
            {            
               Bom bom =(Bom)dataView.SelectedRows[0].Tag;
               int deleteId = bom.getId();
               Bom parentBom = BomDaoImp.getBomById(bom.getParentId());
               BomDaoImp.remove(bom);
               BomForm.updateCost(parentBom);//删除节点后，更新父节点成本值
               int selectId = ((Bom)leftTree.SelectedNode.Tag).getId();
               if (deleteId == selectId)
                   leftTree.SelectedNode = leftTree.SelectedNode.Parent;
               this.refreshCurSelectedNode(leftTree.SelectedNode);
               this.refreshDataTable(leftTree.SelectedNode);
               MessageBox.Show(this, "删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
               
            }
           
        }

        //数据行双击事件响应
        private void dataView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Hashtable ctx = new Hashtable();
            int id = (int)dataView.SelectedRows[0].Cells["id"].Value;
            ctx.Add("id", id);
            ctx.Add("oprtstate", 0); //查看状态
            if (leftTree.SelectedNode == null)
                leftTree.SelectedNode = leftTree.Nodes[0];
            //当前节点参数 
            ctx.Add("curnode", leftTree.SelectedNode);
            openEditUI(ctx);
        }
        //刷新按钮
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            TreeNode curNode = leftTree.SelectedNode;
            refreshCurSelectedNode(curNode);
            refreshDataTable(curNode);
        }
        //搜索按钮
        private void btnSearch_Click(object sender, EventArgs e)
        {
            String content = txtSearch.Text.Trim();
            List<Bom> list = BomDaoImp.getSearchResult(content);
            fillDataListToTable(list, dataView);

        }
        //左侧树双击
        private void leftTree_DoubleClick(object sender, EventArgs e)
        {
            Bom curBom = (Bom)leftTree.SelectedNode.Tag;
            int id = curBom.getId();
            Hashtable ctx = new Hashtable();
            ctx.Add("oprtstate", 0); //查看状态
            if (leftTree.SelectedNode == null)
                leftTree.SelectedNode = leftTree.Nodes[0];
            //当前节点参数 
            ctx.Add("curnode", leftTree.SelectedNode);
            ctx.Add("id", id);
            openEditUI(ctx);
        }
        //退出按钮
        private void btnExit_Click(object sender, EventArgs e)
        {
           DialogResult result =  MessageBox.Show(this,"是否确认要关闭软件？","提示",MessageBoxButtons.OKCancel);
           if (result == DialogResult.OK)
           {
               this.Close();
           }
        }

        private void mainForm_Load(object sender, EventArgs e)
        {

        }



    }
}
